var Log = function () {

    this.$grid_selector = $("#log-grid-table");
    this.pager_selector = "#log-grid-pager";

    this.URL = {
        REMOVE_ALL: '/system/log'
    };

    this.colModel = [
        {name: 'logId', width: 20, key: true},
        {name: 'source'},
        {name: 'username', width: 20},
        {name: 'level', width: 20},
        {name: 'message', hidden: true, width: 20},
        {
            name: 'createdAt', width: 30, formatter: "date",
            formatoptions: {srcformat: 'Y-m-d H:i:s', newformat: 'Y-m-d H:i:s'}
        }
    ];
    this.options = {
        moduleKey: 'system/log',
        pager: this.pager_selector,
        sortname: 'createdAt',
        caption: "日志列表",
        colNames: ['日志ID', '来源', '操作用户', '级别', '详情', '创建时间'],
        colModel: this.colModel,
        navbarDefaultIcon: {
            add: false,
            edit: false,
            view: false
        }
    };
};

Log.prototype = {
    // 初始化表格
    initGird: function () {

        var self = this;
        this.$grid_selector
            .xumf_jqGird(this.options)
            // 编辑权限按钮初始化
            .xumf_jqGird('navGrid', this.pager_selector).jqGrid('navButtonAdd', this.pager_selector,
            {
                caption: '',
                title: "删除全部",
                buttonicon: " fa fa-trash",
                onClickButton: function () {
                    self.deleteAll();
                },
                position: "first"
            })
            .xumf_jqGird('navGrid', this.pager_selector).jqGrid('navButtonAdd', this.pager_selector,
            {
                caption: '',
                title: "查看详情",
                buttonicon: "fa fa-exclamation-circle",
                onClickButton: function () {
                    $(this).getGridSelectedRow(function (rowData) {
                        layer.open({
                            title: '详情',
                            type: 1,
                            btn: ['确定'],
                            area: ['420px', '550px'], //宽高
                            content: '<div style="padding: 12px; white-space: pre-wrap; word-break: break-all; word-wrap: break-word;">' + rowData.message + '</div>'
                        });
                    });
                },
                position: "first"
            });

    },
    /**
     * 删除所有日志
     */
    deleteAll: function () {
        var self = this;
        layer.confirm('<div style="padding:12px;">您确定要清空所有日志吗？</div>', function (index, layero) {
            $.xumf.ajaxAndLoading({
                type: 'DELETE',
                url: self.URL.REMOVE_ALL
            }).success(function (res) {
                if (res.success) {
                    layer.close(index);
                    $('#log-grid-table').trigger('reloadGrid');
                }
                $.xumf.gritter(res);
            });
        });
    }
};

